// 该文件用来创建整个文件的路由器
import VueRouter from 'vue-router'

// 引入有路由关系的组件
import SysLogin from '../pages/SysLogin.vue'
import StudentHome from '../pages/StudentHome.vue';
import SUserCenter from '../pages/SUserCenter';
import SClassCenter from '../pages/SClassCenter';
import StuWelcome from '../pages/StuWelcome';
import AdminHome from '../pages/AdminHome';
import AdmWelcome from '../pages/AdmWelcome';
import AdminTeacher from '../pages/AdminTeacher'
import AdminStudent from '../pages/AdminStudent'
import AdminClass from '../pages/AdminClass'
import AdminCourse from '../pages/AdminCourse'
// 创建并暴露一个路由器，并去管理一组组的路由关系
const router = new VueRouter({

    routes: [
        {
            path:'/',
            redirect:'/login'
        },
        {
            name:'login',
            path:'/login',
            component:SysLogin,  
        },
        {
            name:'studenthome',
            path:'/studenthome',
            component:StudentHome,
            redirect:'/studenthome/stuwelcome',
            children:[
                {
                    name:'stuWelcome',
                    path:'stuwelcome',
                    component:StuWelcome,
                },
                {
                    name:'studentusercenter',
                    path:'susercenter',
                    component:SUserCenter
                    
                },
                {
                    name:'studentclasscenter',
                    path:'sclasscenter',
                    component:SClassCenter
                }
            ]
        },
        {
            name:'adminhome',
            path:'/adminhome',
            component:AdminHome,
            redirect:'/adminhome/admwelcome',
            children:[
                {
                    name:'admwelcome',
                    path:'admwelcome',
                    component:AdmWelcome
                },
                {
                    name:'adteacher',
                    path:'adteacher',
                    component:AdminTeacher
                },
                {
                    name:'adstudent',
                    path:'adstudent',
                    component:AdminStudent
                },
                {
                    name:'adclass',
                    path:'adclass',
                    component:AdminClass
                },
                {
                    name:'adcourse',
                    path:'adcourse',
                    component:AdminCourse
                }
            ]
        }
    ]
})


// 全局前置路由守卫
// 初始化时和每次路由切换之前其回调函数被调用
// 但是如果有大量的路由切换需要判断，就可以在配置项中的meta属性中
// 配置一些我们设定的值用来验证或其他用途，meta叫做路由元数据
// 参数是to，from和next，next()用来放行
// router.beforeEach((to,from,next)=>{
//     if(to.meta.isVerify){
//         if(localStorage.getItem('token') === 'rei'){
//             next()
//         }else{
//             alert('没有token！')
//         }
//     }else{
//         next()
//     }
// })

// 后置路由守卫 初始化时和每次路由成功切换之后其回调函数被调用
// 参数只有to和from
// router.afterEach((to) => {
//     document.title = to.meta.title // 切换成功后切换网页title
// })

router.beforeEach((to,from,next)=>{
	const token = window.sessionStorage.getItem('token');
	if(to.path!=='/login'){
		if(!token){
			next('/login')
		}else{
			next()
		}
	}else{   
		next()
	}
	
})

export default router


// 独享路由守卫
